package com.yc.navy.dayfresh.controller;

import com.yc.navy.dayfresh.biz.TagBiz;
import com.yc.navy.dayfresh.dto.Result;
import com.yc.navy.dayfresh.entity.Fav;
import com.yc.navy.dayfresh.entity.Tag;
import com.yc.navy.dayfresh.entity.User;
import com.yc.navy.dayfresh.mapper.FavMapper;
import com.yc.navy.dayfresh.mapper.TagMapper;
import com.yc.starter.login.config.LoginProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author navy
 * @company 源辰信息
 * @date 2024/9/14
 * @email haijunzhou@hnit.edu.cn
 */
@RestController
@RequestMapping("tag")
public class TagAction {

    @Autowired
    LoginProperties loginProperties;
    @Autowired
    FavMapper favMapper;
    @Autowired
    TagMapper tagMapper;
    @Autowired
    TagBiz tagBiz;
    @RequestMapping("queryByUid")
    List<Tag> queryByUid(HttpSession session){
        User user = (User) session.getAttribute(loginProperties.getSessionKey());
        List<Tag> tags = tagMapper.selectByUid(user.getId());
        List<Fav> fags = favMapper.selectByNoTags(user.getId());
        Tag noTag = new Tag();
        noTag.setName("未分类");
        noTag.setFavs(fags);
        tags.add(0,noTag);
        return tags;
    }

    @RequestMapping("/merge")
    public Result mergeTags(@RequestParam Integer fromTagId, @RequestParam Integer toTagId) {
        tagBiz.mergeTags(fromTagId, toTagId);
        return new Result(1,"合并成功",null);
    }
}
